package controller;

import helper.Formatter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import model.*;
import org.mybeans.dao.DAOException;
import databeans.*;

/*
 * @Xiao Liang
 * Customer Use Case: View Pending Transaction
 */
public class CustomerViewPendingTransactionsAction extends Action{

 
 private TransactionDAO transactionDAO;
 private FundDAO fundDAO;

 public CustomerViewPendingTransactionsAction(Model model) {
  transactionDAO = model.getTransactionDAO();
  fundDAO = model.getFundDAO();
 }

 public String getName() { return "customerViewPendingTransactions.do"; }

 public String perform(HttpServletRequest request) {
    // define target page
	String loginPageAjax   = "common/messages.jsp";
	String successPage = "customer/view-pending-transaction.jsp";
       
    // Set up the errors list
    List<String> errors = new ArrayList<String>();
    List<String> messages = new ArrayList<String>();
    request.setAttribute("messages", messages);
    request.setAttribute("errors",errors);

    try {
        
     //check if customer has actually logged in
     Customer customer = (Customer) request.getSession(true).getAttribute("customer");
     if (customer == null) {
    	 errors.add("Your session has timed out. Please Login again. <a href=\"customerHome.do\">Login</a> ");
         return loginPageAjax;
     }
       
   //get customer id
     int customerID = customer.getCustomerID();
     System.out.println("got customer ID = " + customerID);
     Transaction[] transactions = transactionDAO.getTransactions(customerID);
     
     List<Integer> transIDs = new ArrayList<Integer>();
     
     List<String> transDates_submit = new ArrayList<String>();
     List<String> transTypes = new ArrayList<String>();
     List<String> fundNames = new ArrayList<String>();
     List<Long> numOfShares = new ArrayList<Long>();
     List<Long> amounts = new ArrayList<Long>();
     List<String> statuses = new ArrayList<String>(); 
     SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
     System.out.println("transactions.length = " + transactions.length);
     
     for(int i=0; i<transactions.length; i++){
	      if(transactions[i].getExecuteDate()==null){
	    	  //display fundName
			   int fundID =   transactions[i].getFundID();
			   if (fundID != -1) {
				   Fund fund = fundDAO.lookup(fundID);
				   System.out.println("got fund name = " + fund.getName());
				   fundNames.add(fund.getName());
				   
				   transIDs.add(transactions[i].getTransactionID());
				  
				   //display SubmissionDate
				   transDates_submit.add(format.format(transactions[i].getSubmissionDate()));
				   numOfShares.add(transactions[i].getShares());
				   System.out.println("got shares = " + transactions[i].getShares());
				   
				   //display transactionType
				   transTypes.add(Formatter.transactionType(transactions[i].getTransactionType()));
				   System.out.println("Formatter.transactionType(transactions[i].getTransactionType())" + Formatter.transactionType(transactions[i].getTransactionType())); 
				  
				   //display amount
				   amounts.add(transactions[i].getAmount());
				   System.out.println("got amount = " + transactions[i].getAmount());
				   
				   statuses.add("Pending");
				   System.out.println("add status");
			   }
	      }
     }
     System.out.println("setting attributes");
     request.setAttribute("transIDs", transIDs);
     request.setAttribute("transDates_submit", transDates_submit);
     request.setAttribute("transTypes", transTypes);
     request.setAttribute("fundNames", fundNames);
     request.setAttribute("numOfShares", numOfShares);
     request.setAttribute("amounts", amounts);
     request.setAttribute("statuses", statuses);
     System.out.println("return page");
     return successPage;
    
  }
  catch (DAOException e) {
         errors.add("CustomerViewPendingTransaction(): "+ e.getMessage());
         return successPage;
        }
    }
}